package com.zhao.web.config;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.MultipartConfigElement;

import com.zhao.common.support.SignFilter;
import com.zhao.common.support.SignHandlerInterceptor;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.integration.support.json.Jackson2JsonObjectMapper;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.VersionResourceResolver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhao.interceptor.AppHandlerInterceptor;

/**
 * web config
 * 
 * @author zhaoyongfei
 *
 */
/**
 * @author zhaoyongfei
 *
 */
@Configuration
@EnableWebMvc
@PropertySource("classpath:config/application-web.properties")
public class WebConfig extends WebMvcConfigurerAdapter {

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("index");
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(appHandlerInterceptor());
		registry.addInterceptor(new SignHandlerInterceptor());
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/").resourceChain(true)
				.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd"));
		converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
		// converters.add(new MappingJackson2XmlHttpMessageConverter(builder.xml().build()));
	}

	@Bean
	public AppHandlerInterceptor appHandlerInterceptor() {
		return new AppHandlerInterceptor();
	}

	@Bean
	public Jackson2JsonObjectMapper jackson2JsonObjectMapper() {
		return new Jackson2JsonObjectMapper(objectMapper());
	}

	@Bean
	public ObjectMapper objectMapper() {
		return new ObjectMapper();
	}

	/**
	 * 文件上传配置
	 * 
	 * @return
	 */
	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		factory.setMaxFileSize("128MB");
		factory.setMaxRequestSize("128MB");
		return factory.createMultipartConfig();
	}

	/*
	 * 跨域访问
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/api/**");
	}


	@Bean
	public FilterRegistrationBean heFilterRegistrationBean() {
		FilterRegistrationBean registration = new FilterRegistrationBean(new SignFilter());
		registration.addUrlPatterns("/*");
		return registration;
	}
	/*
	 * @Autowired ApplicationContext applicationContext;
	 * 
	 * @Bean public SpringResourceTemplateResolver templateResolver(){
	 * SpringResourceTemplateResolver templateResolver = new
	 * SpringResourceTemplateResolver();
	 * templateResolver.setApplicationContext(applicationContext);
	 * templateResolver.setPrefix("classpath:/templates/");
	 * templateResolver.setSuffix(".html");
	 * templateResolver.setTemplateMode("HTML5");
	 * templateResolver.setCacheable(true); return templateResolver; }
	 * 
	 * @Bean public SpringTemplateEngine templateEngine(){ SpringTemplateEngine
	 * templateEngine = new SpringTemplateEngine();
	 * templateEngine.setTemplateResolver(templateResolver()); return
	 * templateEngine; }
	 * 
	 * @Bean public ThymeleafViewResolver viewResolver(){ ThymeleafViewResolver
	 * viewResolver = new ThymeleafViewResolver();
	 * viewResolver.setTemplateEngine(templateEngine()); return viewResolver; }
	 */

	//	@Bean 启用AngularJSSEO
	//	public FilterRegistrationBean filterRegistrationBean() {
	//		Map<String, String> initParameters = new HashMap<String, String>();
	//		initParameters.put("phantomjs.binary.path", "phantomjs.exe");
	//		initParameters.put("waitForPageLoad", "3");
	//		initParameters.put("cacheTimeout", "24");
	//		initParameters.put("cachePath", "c:\\cache");
	//		initParameters.put("crawlDepth", "2");
	//		initParameters.put("encoding", "UTF-8");
	//		FilterRegistrationBean registration = new FilterRegistrationBean();
	//		registration.addUrlPatterns("/*");
	//		registration.setFilter(seoFilter());
	//		registration.setName("SEOFilter");
	//		registration.setInitParameters(initParameters);
	//		return registration;
	//	}

	//angularjs fiter
	//	@Bean
	//	public SEOFilter seoFilter() {
	//		SEOFilter seoFilter = new SEOFilter();
	//		return seoFilter;
	//	}

}
